﻿using Magicodes.ExporterAndImporter.Excel;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace FreeSqlDemo.Services
{
    public class XlsxFileResultT<T> : XlsxFileResultBase where T : class, new()
    {
        public XlsxFileResultT(ICollection<T> data, string fileDownloadName = null)
        {
            FileDownloadName = fileDownloadName;
            Data = data;
        }
        public string FileDownloadName { get; }
        public ICollection<T> Data { get; }

        public async override Task ExecuteResultAsync(ActionContext context)
        {
            var exporter = new ExcelExporter();
            var bytes = await exporter.ExportAsByteArray(Data);
            var fs = new MemoryStream(bytes);
            await DownloadExcelFileAsync(context, fs, FileDownloadName);
        }

    }
}
